home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / tctutor2.zip / VC.C < prev    next >
C/C++ Source or Header  |  1987-07-04  |  15KB  |  361 lines

  1. /*         VC.C    VC.C    VC.C    VC.C    VC.C    VC.C    VC.C
  2.  
  3. VISUAL CALCULATOR                           X   X   XXX
  4.   MAIN PROGRAM                              X   X  X   X
  5.                                             X   X  X
  6. July 1, 1987                                 X X   X
  7.                                              X X   X
  8.                                               X    X   X
  9.                                               X     XXX
  10.  
  11.      This program will evaluate single value expressions in a
  12.      manner similar to those evaluated by a hand-held calculator,
  13.      hence its name, the Visual Calculator. It was never intended
  14.      to be programmable, so no loop constructs are included in
  15.      its design. It is possible to write a series of statements,
  16.      store them in a file, and recall them while using them to
  17.      calculate with new values of input variables in the six
  18.      variable storage registers. The input variables can be
  19.      changed, and the entire series recalculated.
  20.  
  21.      Although this is a potentially useful program in its own
  22.      right, it was originally written as an illustration of a
  23.      rather large C program. It is especially useful because
  24.      the student of C can run the program to determine its
  25.      operating characteristics, then study the code needed to
  26.      perform the various operations. For that reason, the entire
  27.      program is heavily commented. An actual production program
  28.      would probably not have as many comments as this example
  29.      but it would not be bad practice to comment all of your
  30.      programs to this extent.
  31. */
  32. #include "ctype.h"
  33. #include "stdio.h"
  34. #include "string.h"
  35. #include "conio.h"
  36. #include "process.h"
  37. #include "struct.def"
  38. #include "defin.h"
  39.  
  40. struct vars allvars[12]; /* this is the main variable storage */
  41. int varinuse = 0;        /* which variable is being used now  */
  42. char inline[200];        /* input line area                   */
  43. int col;                 /* used for searching across the input */
  44. int errcode;             /* error code number                 */
  45. int colerr;              /* column where error occurred       */
  46. int printit = 0;         /* 1 = print a transcript            */
  47. int ignore;              /* 1 = ignore calculations for line  */
  48. extern char strngout[];  /* output message area               */
  49. extern int valattr;      /* value and variable attribute      */
  50. extern int helpattr;     /* help box attribute                */
  51. FILE *prtfile;           /* file pointers                     */
  52.  
  53. struct lines *top, *bot, *q, *p, *arrow, *trnsend;
  54.  
  55. /* *********************************************************** main */
  56. /* This is the main control loop for the program. It initializes    */
  57. /* everything and reads statements until no errors are found. It    */
  58. /* continues reading until an F10 is detected in a subordinate      */
  59. /* function where control is returned to DOS                        */
  60. main()
  61. {
  62.    top = bot = q = p = arrow = trnsend = NULL;
  63.    monitor();                 /* initialize video attributes */
  64.    initdata(&allvars[0]);   /* initialize all data           */
  65.    bkgndvid();   /* display video background - double lines  */
  66.    valusvid();   /* display starting values of all variables */
  67.    strtrans("Welcome to the Visual Calculator - Version 1.10",0);
  68.    transout();
  69.    do{
  70.       poscurs(23,7);
  71.       printf("  input >                                 ");
  72.       printf("                              ");
  73.       do {                     /* repeat input until no errors      */
  74.          readline();           /* get an input line                 */
  75.          errdis("                         ");  /* clear error msg   */
  76.          parse();              /* parse the line                    */
  77.          if (errcode) errout();/* output error message              */
  78.       } while (errcode);
  79.       if (ignore == 1)
  80.          strtrans(inline,0);   /* store comment in transcript       */
  81.       else
  82.          strtrans(inline,1);   /* store "inline" in transcript      */
  83.       transout();
  84.    } while (1);                /* continuous loop                   */
  85. }
  86. /* ******************************************************* readline */
  87. /* This function reads a line by inputting one character at a time  */
  88. /* and deciding what to do with it if it is a special character, or */
  89. /* adding it to the input line if it is a special character. The    */
  90. /* routine takes care of such things as backspace, cursor movement  */
  91. /* and delete keys. The final result is a single line of text stored*/
  92. /* in the buffer "inline" and the line displayed on the monitor.    */
  93. void readline(void)
  94. {
  95. int index;
  96. int c,temp;
  97. int row = 23,col = 17;
  98. int attr;
  99.  
  100.    if (errcode) {                   /* error recovery allow reenter */
  101.       index = colerr;
  102.       errcode = 0;
  103.    }
  104.    else {                                   /* normal input routine */
  105.       index = 0;
  106.       for (temp = 0;temp < 80;temp++)
  107.          inline[temp] = 0;                    /* clear input buffer */
  108.    }
  109.  
  110.    poscurs(row,col+index);           /* starting location of cursor */
  111.  
  112.    do {                /* repeat this do loop until a return is hit */
  113.       while ((c = getch()) == EOF);              /* get a keystroke */
  114.  
  115.       if (c == 0) {       /* a zero here says a special key was hit */
  116.                              /* get the key and act on it as needed */
  117.          int spec;
  118.          spec = getch();          /* this is the special code found */
  119.          switch (spec) {
  120.             case 59 : helpm();                 /* F1 - Help math    */
  121.                       transout();
  122.                       break;
  123.             case 60 : helps();                 /* F2 - Help system  */
  124.                       transout();
  125.                       break;
  126.             case 61 : if (printit) {           /* F3 - Print on/off */
  127.                          printit = 0;          /* print off */
  128.                          fprintf(prtfile,"%s\n\n","Print off");
  129.                          fclose(prtfile);
  130.                          strcpy(strngout,"-----");
  131.                          attr = helpattr;
  132.                       } else {
  133.                          prtfile = fopen("PRN","w");   /* print on */
  134.                          if (prtprblm()) {
  135.                             errcode = 12;   /* printer is not ready */
  136.                             errout();
  137.                             break;
  138.                          }
  139.                          printit = 1;
  140.                          fprintf(prtfile,"%s\n","Print On");
  141.                          strcpy(strngout,"Print");
  142.                          attr = valattr;
  143.                       }
  144.                       strngdis(1,73,attr);
  145.                       break;
  146.             case 62 :                       /* F4 - Mark transcript */
  147.                       arrow->marked = (arrow->marked?0:1);
  148.                       transout();
  149.                       break;
  150.             case 63 : fileout();           /* F5 - Store transcript */
  151.                       break;
  152.             case 64 : filein();             /* F6 - Retrieve trans  */
  153.                       errcode = 0;
  154.                       break;
  155.             case 65 :                       /* F7 -                 */
  156.                       break;
  157.             case 66 :                       /* F8 -                 */
  158.                       break;
  159.             case 67 :                       /* F9 - Edit a line     */
  160.                       strcpy(inline,arrow->lineloc);
  161.                       poscurs(23,17);
  162.                       printf("%s",inline);
  163.                       break;
  164.             case 68 : poscurs(23,17);        /* F10 - Quit to DOS   */
  165.                       printf("Quit? (Y/N) ");
  166.                       c = getch();
  167.                       if ((c == 'Y') || (c == 'y')){
  168.                          clrscrn();
  169.                          exit(0);
  170.                       }
  171.                       poscurs(23,17);
  172.                       printf("              ");
  173.                       break;
  174.             case 75 : if (index) {                    /* left arrow